<template>
  <div id="helpAdd">
    <div id="mainBox">
      <div id="boxTit">
        <span class="tit">创建帮助文档</span>
        <i
          class="el-icon-arrow-up"
          @click="returntop"
        />
      </div>
      <div id="helpmain">
        <el-select
          v-model="value"
          placeholder="请选择"
          class="selects"
          @change="updata"
        >
          <el-option
            v-for="(item,index) in types"
            :key="index"
            :label="item.desc"
            :value="item.name"
          />
        </el-select>
        <el-button @click="btnTxt">保存</el-button>
        <vue-editor
          v-model="content"
          :editor-toolbar="customToolbar"
        />
      </div>
    </div>

  </div>
</template>

<script>
import { doclist, docadd, docwindow } from '@/api/page'
import { VueEditor } from 'vue2-editor'

export default {
  components: {
    VueEditor
  },
  data() {
    return {
      value: '',
      types: [],
      helpArrays: '',
      content: '',
      customToolbar: [
        ['bold', 'italic', 'underline'],
        [{ align: '' }, { align: 'center' }, { align: 'right' }],
        [{ list: 'ordered' }, { list: 'bullet' }, { list: 'check' }],
        [{ background: [] }, { color: [] }],
        ['strike'],
        ['clean']
      ]
    }
  },
  mounted() {
    doclist()
      .then(response => {
        if (response.data.code == 'SUCCESS') {
          // this.types = response.data.data.types.map((item) => {
          //   return JSON.parse(item)
          // })
          this.types = response.data.data.types
        } else {
          alert(response.data.message)
        }
      })
      .catch(error => {
        alert(error.response.data.message)
      })
  },
  methods: {
    updata() {
      var data = []
      data.push(this.value)
      docwindow(data)
        .then(response => {
          if (response.data.code == 'SUCCESS') {
            this.helpArrays = response.data.data
            for (var key in this.helpArrays) {
              this.content = this.helpArrays[key]
            }
          } else {
            alert(response.data.message)
          }
        })
        .catch(error => {
          alert(error.response.data.message)
        })
    },
    returntop() {
      this.$router.push('/help/list')
    },
    btnTxt() {
      var data = {}
      data.hdType = this.value
      data.hdContent = this.content
      docadd(data)
        .then(response => {
          if (response.data.code == 'SUCCESS') {
            this.openFullScreen()
            setTimeout(() => {
              this.loading.close()
            }, 1000)
          } else {
            alert(response.data.message)
          }
        })
        .catch(error => {
          alert(error.response.data.message)
        })
    },
    openFullScreen() {
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    }
  }
}
</script>

<style>
#helpAdd {
  margin-top: 20px;
  margin-left: 20px;
}
#helpAdd .el-input {
  margin-top: 10px;
  margin-right: 10px;
  width: 200px;
}
#helpAdd #mainBox {
  width: 99%;
  border: 1px solid #623f18;
}
#helpAdd #mainBox #boxTit {
  height: 40px;
  width: 100%;
  background: #623f18;
}
#helpAdd #helpmain {
  width: 95%;
  margin-left: 2.5%;
  margin-top: 20px;
}
#helpAdd #helpmain .selects {
  margin-bottom: 20px;
}
#helpAdd #mainBox #boxTit i {
  cursor: pointer;
  float: right;
  line-height: 40px;
  margin-right: 10px;
  font-size: 25px;
  color: #fff;
}
#helpAdd #mainBox #boxTit .tit {
  margin-left: 20px;
  font-size: 20px;
  color: #fff;
  line-height: 40px;
}
#helpAdd .el-input input{
    height: 40px;
    line-height: 40px;
}
#helpAdd .quillWrapper{
    height: 500px;
    padding-bottom: 80px;
}
</style>
